package com.hk.web.context;

import com.hk.commons.exceptions.UserNotExistException;
import com.hk.commons.exceptions.SpecificException;
import com.hk.web.domain.entity.User;
import com.hk.web.domain.lifeCycle.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Antinomy on 17/11/1.
 */
@Service
public class UserContext {
    private Logger logger = LoggerFactory.getLogger(UserContext.class);

    @Autowired
    private UserRepository userRepo;

    public User findUserByName(String name) {
        User user = null;
        try {
            user = userRepo.findByName(name);

            if(user == null){
                throw new SpecificException("用户不存在! mobile=" + name);
            }

        } catch (Exception ex) {
            logger.error(ex.getMessage());
            ex.printStackTrace();
            throw new UserNotExistException("此用户不存在");
        }
        return user;
    }

    public Integer updatePassword(String name, String newPwd) {
        return userRepo.updatePassword(name,newPwd);
    }
}
